﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class LocSite : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chbIsActive;
        protected HtmlForm F2251;
        protected HtmlInputHidden hdnFrom;
        protected Label lblDescr;
        protected Label lblLocSiteID;
        protected Label lblMsg;
        protected Label lblOwner;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtLocSiteID;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtLocSiteID.Text = "";
            this.txtLocSiteID.Enabled = true;
            this.chbIsActive.Checked = false;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtLocSiteID.Text.Trim() != "")
                {
                    List<string> LocSiteIDs = new List<string> {
                        this.txtLocSiteID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(LocSiteIDs, "LOC_SITE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                else
                {
                    this.lblMsg.Text = "没有保存,不能删除";
                }
            }
            catch (Exception)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + "此节点有地址引用,请先删除地址再删除节点。";
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("LocSiteQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("LocSiteList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtLocSiteID.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2540_001");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateLocSiteInfo();
            }
            else
            {
                this.InsertLocSiteInfo();
            }
        }

        private LocSiteInfo GetLocSiteInfo(LocSiteInfo locSiteInfo)
        {
            locSiteInfo.LocSiteID = this.txtLocSiteID.Text.Trim();
            locSiteInfo.Descr = this.txtDescr.Text.Trim();
            locSiteInfo.IsActive = this.chbIsActive.Checked ? "Y" : "N";
            return locSiteInfo;
        }

        private void InsertLocSiteInfo()
        {
            try
            {
                LocSiteInfo locSiteInfo = new LocSiteInfo();
                locSiteInfo = this.GetLocSiteInfo(locSiteInfo);
                locSiteInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(locSiteInfo, "LOC_SITE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = locSiteInfo;
                this.txtLocSiteID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetLocSiteInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLocSiteInfo2Form()
        {
            try
            {
                string LocSiteID = base.Request.QueryString["ID"].ToString();
                LocSiteInfo locSiteInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(LocSiteID, "LOC_SITE") as LocSiteInfo;
                base.DataEntityViewState = locSiteInfo;
                if (locSiteInfo != null)
                {
                    this.txtLocSiteID.Text = locSiteInfo.LocSiteID;
                    this.txtLocSiteID.Enabled = false;
                    this.txtDescr.Text = locSiteInfo.Descr;
                    this.chbIsActive.Checked = locSiteInfo.IsActive == "Y";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateLocSiteInfo()
        {
            try
            {
                LocSiteInfo locSiteInfo = base.DataEntityViewState as LocSiteInfo;
                locSiteInfo = this.GetLocSiteInfo(locSiteInfo);
                locSiteInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(locSiteInfo, "LOC_SITE") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

